package org.example.entity;

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import com.fasterxml.jackson.annotation.JsonFormat;

import javax.persistence.*;
import java.time.LocalDateTime;

/**
 * 服务申请实体类
 */
@Entity
@Table(name = "service_requests")
@EntityListeners(AuditingEntityListener.class)
public class ServiceRequest {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(name = "user_id", nullable = false)
    private Long userId;
    
    @Column(name = "contact_person", nullable = false, length = 100)
    private String contactPerson;
    
    @Column(name = "contact_phone", nullable = false, length = 20)
    private String contactPhone;
    
    @Column(name = "service_address", nullable = false, length = 500)
    private String serviceAddress;
    
    @Column(name = "appointment_time", nullable = false)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm")
    private LocalDateTime appointmentTime;
    
    @Column(name = "device_info", nullable = false, columnDefinition = "TEXT")
    private String deviceInfo;
    
    @Column(name = "problem_description", nullable = false, columnDefinition = "TEXT")
    private String problemDescription;
    
    @Column(name = "urgency_level", length = 20)
    private String urgencyLevel = "URGENT";
    
    @Column(name = "status", length = 20)
    private String status = "PENDING";
    
    @Column(name = "completed_by")
    private Long completedBy;
    
    @Column(name = "completed_at")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime completedAt;
    
    @CreatedDate
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;
    
    @LastModifiedDate
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;
    
    // 构造函数
    public ServiceRequest() {}
    
    public ServiceRequest(Long userId, String contactPerson, String contactPhone, 
                         String serviceAddress, LocalDateTime appointmentTime, 
                         String deviceInfo, String problemDescription, String urgencyLevel) {
        this.userId = userId;
        this.contactPerson = contactPerson;
        this.contactPhone = contactPhone;
        this.serviceAddress = serviceAddress;
        this.appointmentTime = appointmentTime;
        this.deviceInfo = deviceInfo;
        this.problemDescription = problemDescription;
        this.urgencyLevel = urgencyLevel;
    }
    
    // Getter和Setter方法
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public Long getUserId() {
        return userId;
    }
    
    public void setUserId(Long userId) {
        this.userId = userId;
    }
    
    public String getContactPerson() {
        return contactPerson;
    }
    
    public void setContactPerson(String contactPerson) {
        this.contactPerson = contactPerson;
    }
    
    public String getContactPhone() {
        return contactPhone;
    }
    
    public void setContactPhone(String contactPhone) {
        this.contactPhone = contactPhone;
    }
    
    public String getServiceAddress() {
        return serviceAddress;
    }
    
    public void setServiceAddress(String serviceAddress) {
        this.serviceAddress = serviceAddress;
    }
    
    public LocalDateTime getAppointmentTime() {
        return appointmentTime;
    }
    
    public void setAppointmentTime(LocalDateTime appointmentTime) {
        this.appointmentTime = appointmentTime;
    }
    
    public String getDeviceInfo() {
        return deviceInfo;
    }
    
    public void setDeviceInfo(String deviceInfo) {
        this.deviceInfo = deviceInfo;
    }
    
    public String getProblemDescription() {
        return problemDescription;
    }
    
    public void setProblemDescription(String problemDescription) {
        this.problemDescription = problemDescription;
    }
    
    public String getUrgencyLevel() {
        return urgencyLevel;
    }
    
    public void setUrgencyLevel(String urgencyLevel) {
        this.urgencyLevel = urgencyLevel;
    }
    
    public String getStatus() {
        return status;
    }
    
    public void setStatus(String status) {
        this.status = status;
    }
    
    public LocalDateTime getCreatedAt() {
        return createdAt;
    }
    
    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }
    
    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }
    
    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }
    
    /**
     * 获取紧急程度描述
     */
    public String getUrgencyLevelDescription() {
        switch (urgencyLevel) {
            case "GENERAL":
                return "一般";
            case "URGENT":
                return "紧急";
            case "VERY_URGENT":
                return "非常紧急";
            default:
                return "未知";
        }
    }
    
    /**
     * 获取状态描述
     */
    public String getStatusDescription() {
        switch (status) {
            case "PENDING":
                return "待处理";
            case "IN_PROGRESS":
                return "处理中";
            case "COMPLETED":
                return "已完成";
            case "CANCELLED":
                return "已取消";
            default:
                return "未知";
        }
    }
    
    /**
     * 检查是否可以取消
     */
    public boolean canCancel() {
        return "PENDING".equals(status);
    }
    
    /**
     * 检查是否可以完成
     */
    public boolean canComplete() {
        return "IN_PROGRESS".equals(status);
    }
    
    // Getter和Setter方法
    public Long getCompletedBy() {
        return completedBy;
    }
    
    public void setCompletedBy(Long completedBy) {
        this.completedBy = completedBy;
    }
    
    public LocalDateTime getCompletedAt() {
        return completedAt;
    }
    
    public void setCompletedAt(LocalDateTime completedAt) {
        this.completedAt = completedAt;
    }
}
